﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using CustomsSystem.BLL;
using CustomsSystem.Model;
using System.Collections.Generic;
using Maticsoft.Common;

namespace WEB
{
    public partial class workPlanEdit :sheetInfo.sheetEdit
    {
        CustomsSystem.BLL.T_workPlan workPlan = new CustomsSystem.BLL.T_workPlan();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                tbplanDate.Attributes.Add("onClick", "new WdatePicker()");
                lbAddCus.Attributes.Add("onClick", "OpenChildWinCUS()");
                /// <summary>
                /// 判断是否为修改
                /// </summary>
                if (Request.QueryString["ID"] != null)
                {
                    int id = int.Parse(Request.QueryString["ID"]);
                    btnSave.Text = "修改";
                    CustomsSystem.Model.T_workPlan model = new CustomsSystem.Model.T_workPlan();
                    model = workPlan.GetModel(id);

                    /// <summary>
                    /// 用model为各控件赋值
                    /// </summary>
                    tbplanDate.Text = Convert.ToString(model.planDate);

                    CustomerManage cusBLL = new CustomerManage();
                    Customer cusModel = cusBLL.GetModel(model.cID);
                    tbcID.Text = cusModel.CustomerName;

                    tbWorkMan.Text = model.workMan;
                    tbGoodsName.Text = model.goodsName;
                    tbShipName.Text = model.shipName;
                    tbNo.Text = model.No;
                    tbNote.Text = model.note;
                    tbJob.Text = model.job;
                }
                if (Request.UrlReferrer != null)//获取客户端上次请求的url的有关信息
                {
                    ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
                }
                
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            CustomsSystem.Model.T_workPlan model = new CustomsSystem.Model.T_workPlan();

            ///<summary>
            //为model赋值，并进行有效性控制
            /// </summary>
            string strErr = "";  
            if (tbplanDate.Text == "")
            {
                strErr += "日期不能为空\\n";
            }
            else if (!PageValidate.IsDateTime(tbplanDate.Text))
            {
                tbplanDate.BorderColor = System.Drawing.Color.Red;
                strErr += "委托日期格式错误！\\n";
            }
            else
                model.planDate = Convert.ToDateTime(tbplanDate.Text);
            if (tbcID.Text.Trim() == "")
            {
                
                strErr += "请输入客户公司！\\n";
            }
            else
            {
                CustomerManage cusBLL = new CustomerManage();
                Customer cusModel = cusBLL.GetModel(tbcID.Text);
                if (cusModel == null)
                {
                    strErr += " 该客户不存在，请重新输入！\\n";
                }
                else
                    model.cID = Convert.ToInt32(cusModel.ID);
            }
            
            if (!PageValidate.IsNumber(tbNo.Text)&&!PageValidate.IsDecimal(tbNo.Text)&&tbNo.Text!="")
            {
                strErr += "数量栏请输入数字！\\n";
            }
            else
                model.No = tbNo.Text;
            if (tbJob.Text.Trim().Length > 512)
            {
                strErr += "字数超出限制长度！";
            }
            else
                model.job = tbJob.Text;

            if (tbNote.Text.Trim().Length > 512)
            {
                strErr += "字数超出限制长度！";
            }
            else
                model.note = tbNote.Text;

            //提示错误
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            
            model.workMan = tbWorkMan.Text;
            model.goodsName = tbGoodsName.Text;
            model.shipName = tbShipName.Text;
            model.inputDate = DateTime.Now.Date;

            model.inputNo = Session["EmpNo"].ToString();

            //判断按钮是增加按钮还是修改按钮
            if (Request.QueryString["ID"] != null)
            {
                model.ID = Convert.ToInt32(Request.QueryString["ID"]);
                CustomsSystem.BLL.T_workPlan bllChange = new CustomsSystem.BLL.T_workPlan();
                bool resultChange = bllChange.Update(model);
                if (resultChange)
                    MessageBox.ShowAndRedirect(this, "修改成功！",ViewState["UrlReferrer"].ToString());//跳转到修改前的页面
                else
                    MessageBox.Show(this, "修改失败！");
            }
            else
            {
                int result = workPlan.Add(model);

                if (result == 0)
                {
                    MessageBox.Show(this, "增加失败！");
                }
                else
                {
                    MessageBox.Show(this, "增加成功！");
                }
            }
        
        }

    }
}